package com.zjsru.payment.controller;

import com.zjsru.payment.pojo.ProductTransaction;
import com.zjsru.payment.pojo.Result;
import com.zjsru.payment.service.PaymentService;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PaymentController {
    @Resource
    private PaymentService paymentService;



    /**
     * 生成交易记录
     */
    @PostMapping("/payment/transaction")
    @Transactional(rollbackFor = Exception.class)
    public Result createTransaction(@RequestBody ProductTransaction productTransaction){
            paymentService.createTransaction(productTransaction);
            return Result.success("生成成功");
    }
    /**
     * 查询交易所有记录
     */
    @GetMapping("/payment/transaction")
    public Result queryTransaction(){
        try {
            List<ProductTransaction> productTransactions = paymentService.queryTransaction();
            return Result.success(productTransactions);
        }catch (Exception e){
            return Result.error(500, "查询失败:"+e.getMessage());
        }
    }
    /**
     * 通过用户id查询交易记录
     */
    @GetMapping("/payment/transaction/user/{userId}")
    public Result queryTransactionByUserId(@PathVariable("userId") Long userId){
        try {
            List<ProductTransaction> productTransactions = paymentService.queryTransactionByUserId(userId);
            return Result.success(productTransactions);
        }catch (Exception e){
            return Result.error(500, "查询失败:"+e.getMessage());
        }
    }
    /**
     * 退款
     */
    @PostMapping("/payment/refund/{transactionId}")
    @Transactional(rollbackFor = Exception.class)
    public Result refund(@PathVariable("transactionId") Long transactionId){
        paymentService.refund(transactionId);
        return Result.success("退款成功");
    }
}
